package scuba.DiveBuddy;

import android.app.ActionBar;
import android.app.DialogFragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import scuba.DiveBuddy.Adapters.LogEditorAdapter;
import scuba.DiveBuddy.Class.*;
import scuba.DiveBuddy.Core.Core;
import scuba.DiveBuddy.Core.Toolbelt;
import scuba.DiveBuddy.Core.Tuple;
import scuba.DiveBuddy.Fragments.*;

import java.util.Date;

/**
 * @author Alexander Anderson
 * @since 26/2/14
 * @version 2
 */
public class LogEntryEditorActivity extends FragmentActivity
        implements ActionBar.TabListener,
        ViewPager.OnPageChangeListener,
        DatePickerFragment.DateSetListener {

    //Fields
    private int listener;

    //Constructor
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newlogentry);
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        SetHeaderText();

        //Setup Tabs
        ViewPager pager = (ViewPager)findViewById(R.id.tabs);
        LogEditorAdapter adapter = new LogEditorAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

        for (String s : Core.LOGPAGE_TABS)
            getActionBar().addTab(getActionBar().newTab()
                    .setText(s)
                    .setTabListener(this));

        pager.setOnPageChangeListener(this);
    }

    //<editor-fold desc="------> Options Menu">
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.newlog_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId())
        {
            case R.id.action_changedate:
                listener = Core.LISTENER_LOG_DATE;
                DatePickerFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "Dive Date");
                break;

            case R.id.action_cancel:
                finish();
                break;

            case R.id.action_save:
                this.SanitiseAndSave();


            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    //</editor-fold>

    //<editor-fold desc="------> Tab Selection">
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        ((ViewPager)findViewById(R.id.tabs)).setCurrentItem(tab.getPosition());

    }
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }
    //</editor-fold>

    //<editor-fold desc="------> OnPageChangeListener">
    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {
        getActionBar().setSelectedNavigationItem(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
    //</editor-fold>

    @Override
    public void onDateSet(DialogFragment fragment, DateTime date) {
        if(listener == Core.LISTENER_LOG_DATE) {
            LogEntries.getCurrent().setDiveDate(new LocalDate(date));
            this.SetHeaderText();
        }
    }

    //Methods
    private void SetHeaderText() {
        setTitle("Dive " + LogEntries.getCurrent().getDiveNumber() +  " - " +
                Toolbelt.Toolkit.getDateAsString(LogEntries.getCurrent().getDiveDate()));
    }
    private void SanitiseAndSave() {
        /* user has clicked "Save"
          we need to check that all data has been entered into the DiveProfile Tab,
          All other Data is set and is optional to the user
         */


       Tuple<Boolean, String> result = LogPageSanitiser.getInstance().isComplete();
        if(result.x) {
            LogEntries.AssignPressureGroupToCurrent();
            if(LogEntries.getCurrent().Commit())  {
                //Committed to database
                LogEntries.getInstance().InvalidateCache();
                Toolbelt.Toolkit.showToast(getApplicationContext(), "Dive #" + LogEntries.getCurrent().getDiveNumber() + " logged successfully");
                finish(); // close activity
            }
        }
        else {
            //Tell the user what hasn't been set
            Toolbelt.Toolkit.showToast(getApplicationContext(), result.y);
        }
    }
}